<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增项目')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-project-add">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">名称：</label>
            <div class="col-sm-8">
                <input id="proName" name="proName" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <input name="proDes" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
                    <input type="radio" th:id="${'proStatus_' + dict.dictCode}" name="proStatus" th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'proStatus_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">开始日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <input name="startTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">结束日期：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <input name="endTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>

        <!--<div class="form-group">
            <label class="col-sm-3 control-label is-required">分析经理：</label>
            <div class="col-sm-8">
                <select id="select-managers" name="managers" class="form-control selectpicker" data-none-selected-text="请选择分析经理(可多选)" multiple>
                    <option th:each="manager:${managers}" th:value="${manager.userId}" th:text="${manager.loginName}" ></option>
                </select>
            </div>
        </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">分析专家：</label>
            <div class="col-sm-8">
                <select id="select-verifiers" name="verifiers" class="form-control selectpicker" data-none-selected-text="请选择分析专家(可多选)" multiple>
                    <option th:each="verifier:${verifiers}" th:value="${verifier.userId}" th:text="${verifier.loginName}" ></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">分析员：</label>
            <div class="col-sm-8">
                <select id="select-requesters" name="requesters" class="form-control selectpicker" data-none-selected-text="请选择分析员(可多选)" multiple>
                    <option th:each="requester:${requesters}" th:value="${requester.userId}" th:text="${requester.loginName}" ></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">仪器：</label>
            <div class="col-sm-8">
                <select id="select-instruments" name="instruments" class="form-control selectpicker" data-none-selected-text="请选择仪器(可多选)" onchange="showServices()" multiple>
                    <option th:each="instrument:${instruments}" th:data-subtext="${instrument.hostId}" th:value="${instrument.instId}" th:text="${instrument.instName}" th:disabled="${instrument.status != 0}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">服务：</label>
            <div class="col-sm-8">
                <select id="select-services" name="services" class="form-control selectpicker" data-size="8" data-actions-box="true" data-none-selected-text="请选择服务(可多选)" multiple>

                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/project";

    $("#form-project-add").validate({
        onkeyup: false,
        rules: {
            proName: {
                remote: {
                    url: prefix + "/checkProNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "proName": function () {
                            return $.common.trim($("#proName").val());
                        }
                    }
                }
            }
        },
        messages: {
            "proName": {
                remote: "该项目已经存在",
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        //下拉框必选项校验
        var verifiers = $("#select-verifiers").val();
        if($.common.isEmpty(verifiers)){
            $.modal.alertError("请选择分析专家");
            return false;
        }
        var requesters = $("#select-requesters").val();
        if($.common.isEmpty(requesters)){
            $.modal.alertError("请选择分析员");
            return false;
        }
        if ($.validate.form()) {
            var data = $('#form-project-add').serializeArray();
            // alert(JSON.stringify(data));
            $.operate.save(prefix + "/add", data);
        }
    }

    $("input[name='startTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='endTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    //选择仪器
    function showServices() {
        var instIds = $('#select-instruments').val();
        $("#select-services").empty();
        $("#select-services").selectpicker("refresh");
        $.each(instIds, function(index, v){
            var config = {
                url: prefix + "/showServices",
                type: "post",
                dataType: "json",
                data: {"instIds": v},
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍候...");
                },
                success: function(result) {
                    //回调事件
                    var options = [];
                    $.each(result.data, function (index, value) {
                        if(value.serStatus != 0){
                            options.push('<option data-subtext="'+value.serDes+'" value="' + value.serId + '" disabled="disabled">' + value.serName + '</option>');
                        }else {
                            options.push('<option data-subtext="'+value.serDes+'" value="' + value.serId + '">' + value.serName + '</option>');
                        }
                    })
                    var group = '<optgroup label="--  '+result.data[0].instrument.instName+'  --">'+options.join("")+'</optgroup>';
                    $("#select-services").append(group);
                    $("#select-services").selectpicker('val', '');
                    $("#select-services").selectpicker("refresh");
                    $.modal.closeLoading();
                    // $.operate.successTabCallback(result); 不需要刷新
                }
            };
            $.ajax(config)
        })
    }
</script>
</body>
</html>